@{
    ViewData["Title"] = ViewBag.Title ?? "权限编辑";
    Layout = null;
}
<link href="~/layui/css/layui.css" rel="stylesheet" />
<body>
    <div class="layui-container">
        <div class="layui-card">
            <div class="layui-card-header">
                <h2>@ViewBag.Title</h2>
            </div>
            <div class="layui-card-body">
                <form class="layui-form" lay-filter="permissionForm" id="permissionForm" method="post">
                    <div class="layui-form-item" id="idFormItem" style="@(ViewBag.IsEdit ? "" : "display:none;")">
                        <label class="layui-form-label">ID</label>
                        <div class="layui-input-block">
                            <input type="text" name="Id_display" id="permissionIdDisplay" value="@ViewBag.Permission?.Id" readonly class="layui-input">
                        </div>
                    </div>
                    <!-- 使用隐藏字段存储ID，确保值正确传递 -->
                    <input type="hidden" name="Id" id="permissionId" value="@(ViewBag.Permission?.Id ?? 0)">
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">权限名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="PermissionName" required lay-verify="required" placeholder="请输入权限名称" autocomplete="off" class="layui-input" value="@ViewBag.Permission?.PermissionName">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit lay-filter="savePermission">保存</button>
                            <button type="button" class="layui-btn layui-btn-primary" id="btnCancel">取消</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</body>
<script src="~/layui/layui.js"></script>
<script>
    layui.use(['form', 'layer'], function () {
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.$;
        
        // 初始化表单
        form.render();
        
        // 取消按钮点击事件
        $('#btnCancel').on('click', function () {
            window.location.href = '/Permission/ShowPermission';
        });
        
        // 监听表单提交
        form.on('submit(savePermission)', function (data) {
            var formData = data.field;
            console.log("提交表单数据:", formData);
            
            // 获取ID和权限名称
            var id = parseInt(formData.Id);
            var permissionName = formData.PermissionName;
            
            console.log("解析的ID值:", id, "权限名称:", permissionName);
            
            var isNew = id === 0;
            
            // 权限名称不能为空
            if (!permissionName || permissionName.trim() === '') {
                layer.msg('权限名称不能为空');
                return false;
            }
            
            // 检查ID是否存在且是否为有效值
            if (!isNew && (isNaN(id) || id <= 0)) {
                layer.msg('无效的权限ID，无法保存');
                console.error("保存时ID无效:", formData.Id);
                return false;
            }

            // 根据是新建还是编辑选择不同的URL
            var url = isNew ? '/Permission/CreatePermission' : '/Permission/UpdatePermission';
            var data = isNew ? 
                { permissionName: permissionName } : 
                { id: id, permissionName: permissionName };
                
            // 发送AJAX请求
            $.ajax({
                url: url,
                type: 'POST',
                contentType: 'application/x-www-form-urlencoded',
                data: data,
                success: function(res) {
                    console.log("操作响应:", res);
                    if (res && res.success) {
                        layer.msg(isNew ? '添加成功' : '更新成功', {
                            time: 1000
                        }, function() {
                            // 操作成功后返回列表页
                            window.location.href = '/Permission/ShowPermission';
                        });
                    } else {
                        layer.msg(res && res.message ? res.message : (isNew ? '添加失败' : '更新失败'));
                    }
                },
                error: function(xhr) {
                    console.error("操作失败:", xhr.responseText);
                    layer.msg(isNew ? '添加失败，请重试' : '更新失败，请重试');
                }
            });
            
            return false; // 阻止表单默认提交
        });
    });
</script> 